package com.android.job.ui;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;

import com.android.job.fusion.FusionCode;
import com.android.job.fusion.FusionField;
import com.android.job.logic.LoginLogic;
import com.android.job.model.LoginInfo;

/**
 * 登录页面
 * 
 * @author Administrator
 * 
 */
public class LoginActivity extends BaseActivity implements OnClickListener
{
	private EditText userName;
	private EditText userPass;
	private CheckBox autoLogin;
	private Button registerBtn;
	private Button loginBtn;
	private ProgressDialog pd;
	private SharedPreferences sharedPreferences;
	private ImageView search_back;

	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.login);
		initViews();
	}

	private void initViews()
	{
		userName = (EditText) findViewById(R.id.userName);
		userPass = (EditText) findViewById(R.id.password);
		// autoLogin = (CheckBox) findViewById(R.id.auto_checkbox);
		registerBtn = (Button) findViewById(R.id.register_btn);
		loginBtn = (Button) findViewById(R.id.login_btn);
		search_back = (ImageView) findViewById(R.id.search_back);
		registerBtn.setOnClickListener(this);
		loginBtn.setOnClickListener(this);
		search_back.setOnClickListener(this);
		pd = new ProgressDialog(LoginActivity.this);
		pd.setCancelable(true);
		pd.setMessage(getString(R.string.logining));
		
		sharedPreferences = getSharedPreferences(FusionField.POSTARR,
				Activity.MODE_PRIVATE);
	}

	private void login()
	{
		String name = userName.getText().toString().trim();
		String password = userPass.getText().toString().trim();

		if (name == null || "".equals(name))
		{
			Toast.makeText(this, R.string.userName_null, Toast.LENGTH_SHORT)
					.show();
			return;
		}
		if (password == null || "".equals(password))
		{
			Toast.makeText(this, R.string.userPassword_null, Toast.LENGTH_SHORT)
					.show();
			return;
		}

		if (name != null && !"".equals(name) && password != null
				&& !"".equals(password))
		{
			pd.show();
			LoginLogic.getInstance().login(handler, name, password);
			// LoginLogic.getInstance().login(handler, name, password);
		}
	}
	
	private void saveData(LoginInfo data)
	{
		if(sharedPreferences!=null)
		{
			SharedPreferences.Editor editor = sharedPreferences.edit();
			editor.putString("postArr",data.json);
			editor.putString("type",data.userType);
			editor.putString("id",data.userId);
			editor.commit();
		}
		
	}

	private Handler handler = new Handler()
	{

		public void handleMessage(Message msg)
		{
			switch (msg.what)
			{
				case FusionCode.RETURN_JSONOBJECT:
					pd.dismiss();
					LoginInfo info = (LoginInfo) msg.obj;
					saveData(info);
					String realNameSucc = info.realName + getString(R.string.login_success);
					Toast.makeText(LoginActivity.this, realNameSucc,
							Toast.LENGTH_SHORT).show();
					FusionField.islogin = true;
					finish();
					break;
				case FusionCode.NETWORK_ERROR:
					pd.dismiss();
					Toast.makeText(LoginActivity.this, R.string.network_error,
							Toast.LENGTH_SHORT).show();
					break;
				case FusionCode.NETWORK_TIMEOUT_ERROR:
					pd.dismiss();
					Toast.makeText(LoginActivity.this,
							R.string.network_timeout, Toast.LENGTH_SHORT)
							.show();
					break;
				case FusionCode.SERVER_ERROR:
					pd.dismiss();
					String errorInif = (String) msg.obj;
					
					Toast.makeText(LoginActivity.this,
							errorInif, Toast.LENGTH_SHORT)
							.show();
					break;
				default:
					break;
			}
		};

	};

	@Override
	public void onClick(View v)
	{
		switch (v.getId())
		{
			case R.id.register_btn:
				jumpToPage(RegisterActivity.class, null, true);
				break;

			case R.id.login_btn:

				login();
				break;
			case R.id.search_back:

				finish();
				break;
			default:
				break;
		}
	}

}
